
loc <-  "C:/cygwin/home/patc/rp4/loansim/"

outfile <- paste(c(loc,"out"),collapse="")

library(zoo)
hdr <- c("step","streamid","total","solvent","divs","nim","roe","roa","timetofail","equity","capratio")
cols <- c("integer","integer","integer","integer","numeric","numeric","numeric","numeric","numeric","numeric","numeric")
dat <- read.table(outfile,colClasses=cols)

datz <- as.zoo(as.matrix(dat))
colnames(datz) <- hdr



plot(na.locf(as.zoo(dat)))
databline(h=10)
points(as.zoo(lowess(na.approx(dat$V4),f=0.025)$y),pch=".",col=2)

abline(h=seq(10,80,10),col=3,lty=3)
points(dat$V3,pch=".",col=4)
points(lowess(dat$V4,f=0.025),pch=".",col=6)

points((lowess(dat$V3,f=0.025)$y- 15) * 5 + 30,pch=".",col=7)

dd <- dat[dat$V4 > 500,]

plot(dd$V4 ~ dd$V3)

mod <- lm(dd$V4 ~ dd$V3)

locyc <- function(x) { paste(c(loc,"yieldcurve-",x,".lyc"),collapse="") }

ycurve <- function(t){
  ycfile <- locyc(t)

  ychdr <- c("term","rate","result","party")
  yccols <- c("numeric","numeric","logical","factor")
  ycdat <- read.table(ycfile,colClasses=yccols)

  colnames(ycdat) <- ychdr
  return (ycdat)
}

plotyc <- function(yc){
  xl = c(-0.5,5.5)
  yl = c(-0.01,0.11)

  plot(yc[yc$result==T&yc$party=="Borrower",1:2],xlim=xl,ylim=yl,col=2,pch=".",
        main="Loan Yield Curve")
  points(yc[yc$result==F&yc$party=="Borrower",1:2],pch=".")

  plot(yc[yc$result==T&yc$party=="Depositor",1:2],xlim=xl,ylim=yl,col=2,pch=".",
        main="Deposit Yield Curve")
  points(yc[yc$result==F&yc$party=="Depositor",1:2],pch=".")
}

plotycvec <- function(vec){            
  par(mfrow=c(length(vec),2))                       
  for(i in vec){
    print(i)
    yc <- ycurve(i)
    plotyc(yc)
  }
}

